package br.com.canvasForAll.model.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtil {
	
	/** 
	 * CONSTANTES PARA ALGORITMOS DE ENCRIPTACAO 
	 * */
	private static final String MD5 = "MD5";
	private static final String SHA1 = "SHA-1";
	private static final String SHA256 = "SHA-256";
	
	public static String gerarHash(String str) throws UnsupportedEncodingException{
		try {
			return gerarHexa(gerarHashSha1(str));

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unused")
	private static byte[] gerarHashMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest hash = MessageDigest.getInstance(MD5);
		hash.update(str.getBytes("UTF-8"));
		return hash.digest();
	}
	
	private static byte[] gerarHashSha1(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest hash = MessageDigest.getInstance(SHA1);
		hash.update(str.getBytes("UTF-8"));
		return hash.digest();
	}
	
	@SuppressWarnings("unused")
	private static byte[] gerarHashSha256(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest hash = MessageDigest.getInstance(SHA256);
		hash.update(str.getBytes("UTF-8"));
		return hash.digest();
	}
	
	private static String gerarHexa(byte[] hash){
		StringBuilder hexString = new StringBuilder();

		for (byte b : hash)
		  hexString.append(String.format("%02X", 0xFF & b));

		return hexString.toString();
	}
}
